const http = require('http');

// 测试AI API
function testAIAPI() {
  const postData = JSON.stringify({
    message: '你好，请介绍一下你自己'
  });

  const options = {
    hostname: 'localhost',
    port: 8083,
    path: '/XinAi/chat',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(postData)
    }
  };

  const req = http.request(options, (res) => {
    console.log(`状态码: ${res.statusCode}`);
    console.log(`响应头: ${JSON.stringify(res.headers)}`);
    
    let data = '';
    
    res.on('data', (chunk) => {
      data += chunk;
    });
    
    res.on('end', () => {
      try {
        const response = JSON.parse(data);
        console.log('\n=== AI API 响应 ===');
        console.log('成功状态:', response.success);
        console.log('消息:', response.message);
        console.log('用户消息:', response.userMessage);
        console.log('AI回复长度:', response.aiResponse ? response.aiResponse.length : '无');
        console.log('AI回复前100字符:', response.aiResponse ? response.aiResponse.substring(0, 100) : '无');
        console.log('耗时:', response.duration, 'ms');
        console.log('\n完整响应:');
        console.log(JSON.stringify(response, null, 2));
        
        // 检查AI回复内容
        if (response.success && response.aiResponse) {
          console.log('\n✅ AI回复正常，内容长度:', response.aiResponse.length);
          if (response.aiResponse.length < 50) {
            console.log('⚠️ 警告：AI回复内容可能过短');
          }
        } else {
          console.log('\n❌ AI回复异常');
        }
        
      } catch (error) {
        console.error('解析响应失败:', error);
        console.log('原始响应数据:', data);
      }
    });
  });

  req.on('error', (error) => {
    console.error('请求失败:', error);
  });

  req.write(postData);
  req.end();
}

console.log('开始测试AI API...');
console.log('发送消息: 你好，请介绍一下你自己');
console.log('目标地址: http://localhost:8083/XinAi/chat\n');

testAIAPI(); 